import 'package:flutter/material.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/model/love_record_model.dart' as LRModel;
import 'package:love_app/providers/love_record_provider.dart';
import 'package:love_app/routes/application.dart';
import 'package:provider/provider.dart';

// // 根据图片数组长度返回不同布局ui
// class ImageLengthUi {
// }

// 根据图片数组长度返回不同布局ui

BuildContext _context;
class ImageLengthUI extends StatelessWidget {
  // final List<LRModel.Pictures> imagesList;
  final List imagesList;
  final String itemId;

  ImageLengthUI({@required this.imagesList, @required this.itemId});

  @override
  Widget build(BuildContext context) {
    _context = context;
    return _imageUI();
  }

  // 生成每一个item内容
  Widget _imageUI() {
    Widget widget;
    int imagesLength = this.imagesList.length;
    switch (imagesLength) {
      case 0:
        widget = _imagesLengthIs0();
        break;
      // case 1:
      //   widget = _imagesLengthIs1(); // 暂时先不处理一张和多张图片区别
      //   break;
      default:
        // 两张到九张使用warp样式
        widget = _imagesLengthIs2_9();
    }
    return widget;
  }

  // 数组长度为空的时候直接返回一个空容器
  Widget _imagesLengthIs0() {
    return Container();
  }

  // 数组长度为1的时候布局
  Widget _imagesLengthIs1() {
    LRModel.Pictures item = this.imagesList.first;
    return Container(
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(
        left: Su().w(13),
      ),
      child: InkWell(
        onTap: () {},
        child: Image.network(
          item.thumbnail,
          fit: BoxFit.fitHeight,
        ),
      ),
    );
  }

  Widget _imagesLengthIs2_9() {
    return Container(
      width: Su().w(750),
      child: Wrap(
        spacing: Su().w(13), // 主轴(水平)方向间距
        runSpacing: Su().w(13), // 纵轴（垂直）方向间距
        alignment: WrapAlignment.start, //沿主轴方向居中
        crossAxisAlignment: WrapCrossAlignment.start, //
        children: _getWardChildren(),
      ),
    );
  }

  // 获取所有的warp布局内容
  List<Widget> _getWardChildren() {
    List<Widget> list = [];
    int index = 0;
    for (dynamic item in this.imagesList) {
      list.add(_imageItem(item, index));
      index++;
    }
    return list;
  }

  // 获取单独每个图片布局
  Widget _imageItem(dynamic item, int intIndex) {
    return InkWell(
      onTap: () {
        String index = intIndex.toString();
        Application.router.navigateTo(_context, "/showImagesUI?itemId=$itemId&index=$index");
      },
      child: Container(
        child: Image.network(
          item.thumbnail,
          fit: BoxFit.fitWidth,
          height: Su().w(219),
          width: Su().w(219),
        ),
      ),
    );
  }
}
